package com.example.idachuappone.index.ui;

import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class ChildViewPager extends ViewPager {
    PointF curP;
    PointF downP;
    OnSingleTouchListener onSingleTouchListener;

    public static interface OnSingleTouchListener {
        void onSingleTouch();
    }

    public ChildViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.downP = new PointF();
        this.curP = new PointF();
    }

    public ChildViewPager(Context context) {
        super(context);
        this.downP = new PointF();
        this.curP = new PointF();
    }

    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        return true;
    }

    public boolean onTouchEvent(MotionEvent arg0) {
        this.curP.x = arg0.getX();
        this.curP.y = arg0.getY();
        if (arg0.getAction() == 0) {
            this.downP.x = arg0.getX();
            this.downP.y = arg0.getY();
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        if (arg0.getAction() == 2) {
            if (this.curP.y - this.downP.y > 100.0f) {
                getParent().requestDisallowInterceptTouchEvent(false);
                return true;
            }
            int childCount = getChildCount();
            if (childCount == 1) {
                getParent().requestDisallowInterceptTouchEvent(false);
            } else if (childCount >= 2) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
        }
        if (arg0.getAction() != 1 || Math.abs(this.downP.x - this.curP.x) >= 5.0f || Math.abs(this.downP.y - this.curP.y) >= 5.0f) {
            return super.onTouchEvent(arg0);
        }
        onSingleTouch();
        return true;
    }

    public void onSingleTouch() {
        if (this.onSingleTouchListener != null) {
            this.onSingleTouchListener.onSingleTouch();
        }
    }

    public void setOnSingleTouchListener(OnSingleTouchListener onSingleTouchListener) {
        this.onSingleTouchListener = onSingleTouchListener;
    }
}
